//
//  SNSummaryInfoCell.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/28.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "UIImageView+TapGesture.h"
#import "IQUIView+IQKeyboardToolbar.h"
#import "SNFertilizerInformationCell.h"
//=****** add class ****************** stone ***
static SNProductionPlanListFertlist * __productionPlanListFertlist;
static NSInteger __sectionZeroType;

@implementation SNFertilizerInformationButton

-(void)setHighlighted:(BOOL)highlighted{
    
}

-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
    
    NSInteger fertTypes = 0;
    if ([self.titleLabel.text isEqualToString:@"单体肥"]) {
        fertTypes = 0;
    }else if ([self.titleLabel.text isEqualToString:@"复合肥"]) {
        fertTypes = 1;
    }else if ([self.titleLabel.text isEqualToString:@"掺混肥"]) {
        fertTypes = 2;
    }
    
    switch (self.indexPath.section) {
        case 0:
            self.textField.placeholder = @"肥料种类";
            [self.pickerView selectRow:fertTypes inComponent:0 animated:NO];
            [self.pickerView reloadAllComponents];
            break;
        case 2:
            [self.pickerView selectRow:0 inComponent:0 animated:NO];
            [self.pickerView reloadAllComponents];
            self.textField.placeholder = @"含量";
            break;
            
        default:
            break;
    }
}
- (void)cancelAction:(id)sender
{
    [self.textField resignFirstResponder];
}
- (void)doneAction:(id)sender
{
    NSLog(@"%@ | %s",self.class,__func__);
    UIWindow * window = [UIApplication sharedApplication].delegate.window;
    UIView * view = [window viewWithTag:444];
    [view removeFromSuperview];
    
    
    if (self.textField.text.length <= 0) {
        
        if (self.titleLabel.text.length <= 0) {
            self.titleLabel.text = @"单体肥";
        }
        
        self.textField.text = self.titleLabel.text;
    }
    
    !self.doneBlock ?: self.doneBlock(self.textField);
    
    [self.textField resignFirstResponder];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        UITextField * textField = [UITextField new];
        self.textField = textField;
        textField.placeholder = @"肥料种类";
//        [textField setCustomDoneTarget:self action:@selector(doneAction:)];
        
        /** add pick */
        UIPickerView *pickerView = nil;
        {
            pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
            pickerView.backgroundColor = [UIColor whiteColor];
            pickerView.showsSelectionIndicator = YES;
            pickerView.delegate = self;
            pickerView.dataSource = self;
        }
        self.pickerView = pickerView;
        textField.inputView = pickerView;
        
        [self addSubview:textField];
        
        [textField addLeftRightOnKeyboardWithTarget:self leftButtonTitle:@"取消" rightButtonTitle:@"确定" leftButtonAction:@selector(cancelAction:) rightButtonAction:@selector(doneAction:) shouldShowPlaceholder:YES];
    }
    return self;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (self.indexPath.section) {
        case 0:
            
            return 3;
        case 2:
            
            return 4;
            
        default:
            return 3;
    }
    
}

- (NSArray<NSString *>*)getFertContent
{
    if(__sectionZeroType == 0){
        return @[@"氮肥-46%氮",@"二胺-18%氮，46%磷",@"氯化钾-60%钾",@"硫酸钾-50%钾"];
    }else if(__sectionZeroType == 1){
        return @[@"15%氮，15%磷，15%钾",
                 @"17%氮，17%磷，17%钾",
                 @"18%氮，18%磷，18%钾",
                 @"其他"];
    }else if(__sectionZeroType == 2){
        return @[@"15%氮，15%磷，15%钾",
                 @"17%氮，17%磷，17%钾",
                 @"18%氮，18%磷，18%钾",
                 @"其他"];
    }else{
        return @[@"15%氮，15%磷，15%钾",
                 @"17%氮，17%磷，17%钾",
                 @"18%氮，18%磷，18%钾",
                 @"其他"];
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    switch (self.indexPath.section) {
        case 0:
            return @[@"单体肥",@"复合肥",@"掺混肥"][row];
        case 2:
            return [self getFertContent][row];
        default:
            return @[@"单体肥",@"复合肥",@"掺混肥"][row];
    }
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    
    switch (self.indexPath.section) {
        case 0:
            __sectionZeroType = row;
            self.textField.text = @[@"单体肥",@"复合肥",@"掺混肥"][row];
            break;
        case 2:
            
            self.textField.text =[self getFertContent][row];
            break;
        default:
            self.textField.text =[self getFertContent][row];
            break;
    }
}
@end
//=************************ stone ***
@interface SNFertilizerInformationCell()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;


@property (weak, nonatomic) IBOutlet UILabel *importanceLabel;
/** View */
@property (weak, nonatomic) UIView * bgView;
/** previousTypeName */
@property (nonatomic,copy) NSString * previousTypeName;
@end
@implementation SNFertilizerInformationCell

- (void)doneAction:(id)sender
{
    [self.rightTextField resignFirstResponder];
}
- (void)cancelAction:(id)sender
{
    [self.rightTextField resignFirstResponder];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.contentView.backgroundColor = HexRGB(0xffffff);
        
        
        
    }
    return self;
}

- (IBAction)iconButtonClicked:(SNFertilizerInformationButton *)sender {
    
    UITableView * tableView = (UITableView *)self.superview.superview;
    SNFertilizerInformationCell * cell =  (SNFertilizerInformationCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    if(cell.iconButton.currentTitle.length <=0 && self.indexPath.section == 2){
        NSString * title = @"";
        NSString * message = @"请先选择肥料种类";
        NSString * actionButtonTitle = @"确定";
        
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction * action = [UIAlertAction actionWithTitle:actionButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // 确定之后需要 处理的code
            
        }];
        
        [alertController addAction:action];
        
        [self.vc presentViewController:alertController animated:YES completion:nil];
        return ;
    }
    
    
    
    UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.bgView = bgView;
    self.bgView.tag = 444;
    bgView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    
    UIWindow * window = [UIApplication sharedApplication].delegate.window;
    
    [window addSubview:bgView];
    
    @weakify(bgView)
    bgView.sn_tapBlock = ^(){
        @strongify(bgView) //strong_bgView
        [strong_bgView removeFromSuperview];
        [sender.textField resignFirstResponder];
    };
    
    NSLog(@"%@ | %s",self.class,__func__);
    [sender.textField becomeFirstResponder];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    @weakify_self
    self.iconButton.doneBlock = ^(UITextField * textField){
        @strongify_self
        self.iconButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.iconButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.iconButton setTitle:textField.text forState:UIControlStateNormal];
        [self.iconButton setImage:nil forState:UIControlStateNormal];
        if (self.indexPath.section == 0) {
            if([self.previousTypeName isEqualToString:textField.text]){
                // 没变
            }else{
                [self foo:textField];
                self.previousTypeName = textField.text;
            };
        }else{
            self.iconButton.textField.text = textField.text;
        }
    };
    [self.rightTextField addLeftRightOnKeyboardWithTarget:self leftButtonTitle:@"取消" rightButtonTitle:@"确定" leftButtonAction:@selector(cancelAction:) rightButtonAction:@selector(doneAction:) shouldShowPlaceholder:YES];
    
    
}
- (void)foo:(UITextField *)textField
{
    UITableView * tableView = (UITableView *)self.superview.superview;
    SNFertilizerInformationCell * cell =  (SNFertilizerInformationCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]];
    [cell.iconButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    NSString * fertContentName;
    if ([self.iconButton.titleLabel.text isEqualToString:@"单体肥"]) {
        fertContentName = [SNUtils getFertContentNameWithFertTypes:@"1" fertContent:@"1"];
    }else if ([self.iconButton.titleLabel.text isEqualToString:@"复合肥"]) {
        fertContentName = [SNUtils getFertContentNameWithFertTypes:@"2" fertContent:@"1"];
    }else if ([self.iconButton.titleLabel.text isEqualToString:@"掺混肥"]) {
        fertContentName = [SNUtils getFertContentNameWithFertTypes:@"3" fertContent:@"1"];
    }
    
    [cell.iconButton setTitle:fertContentName forState:UIControlStateNormal];
    [cell.iconButton setImage:nil forState:UIControlStateNormal];
    
}

- (void)configCellWithIndexPath:(NSIndexPath *)indexPath productionPlanListFertlist:(SNProductionPlanListFertlist *)productionPlanListFertlist;
{
    _indexPath = indexPath;
    _productionPlanListFertlist = productionPlanListFertlist;
    __productionPlanListFertlist = productionPlanListFertlist;
    self.iconButton.indexPath = indexPath;
    
    switch (indexPath.section) {
        case 1:
        case 5:
            self.rightTextField.keyboardType = UIKeyboardTypeDecimalPad;
            break;
        default:
            self.rightTextField.keyboardType = UIKeyboardTypeDefault;
            break;
    }
    
    switch (indexPath.section) {
        case 0:
            self.titleLabel.text = @"肥料种类";
            self.iconButton.hidden = NO;
            
            if (productionPlanListFertlist.fertTypesName.length > 0) {
                self.iconButton.titleLabel.font = [UIFont systemFontOfSize:14];
                
                [self.iconButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [self.iconButton setTitle:productionPlanListFertlist.fertTypesName forState:UIControlStateNormal];
                
                [self.iconButton setImage:nil forState:UIControlStateNormal];
                
                self.previousTypeName = productionPlanListFertlist.fertTypesName;
            }else{
                [self.iconButton setImage:[UIImage imageNamed:@"06-next"] forState:UIControlStateNormal];
                [self.iconButton setTitleColor:nil forState:UIControlStateNormal];
                [self.iconButton setTitle:nil forState:UIControlStateNormal];
            }
            
            self.rightTextField.hidden = YES;
            self.importanceLabel.hidden = NO;
            break;
        case 1:
            self.titleLabel.text = @"肥料总量";
            
            self.iconButton.hidden = YES;
            self.rightTextField.hidden = NO;
            self.rightTextField.placeholder = @"吨";
            self.rightTextField.text = productionPlanListFertlist.fertTotal;
            self.importanceLabel.hidden = NO;
            break;
        case 2:
            self.titleLabel.text = @"含量";
            
            self.iconButton.hidden = NO;
            if (productionPlanListFertlist.fertTypesName.length > 0) {
                self.iconButton.titleLabel.font = [UIFont systemFontOfSize:14];
                [self.iconButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                NSString * fertContentName = [SNUtils getFertContentNameWithFertTypes:productionPlanListFertlist.fertTypes fertContent:productionPlanListFertlist.fertContent];
                [self.iconButton setTitle:fertContentName forState:UIControlStateNormal];
                [self.iconButton setImage:nil forState:UIControlStateNormal];
            }else{
                [self.iconButton setImage:[UIImage imageNamed:@"06-next"] forState:UIControlStateNormal];
                [self.iconButton setTitleColor:nil forState:UIControlStateNormal];
                [self.iconButton setTitle:nil forState:UIControlStateNormal];
            }
            self.rightTextField.hidden = YES;
            self.importanceLabel.hidden = YES;
            
            break;
        case 3:
            self.titleLabel.text = @"品牌";
            
            self.iconButton.hidden = YES;
            self.rightTextField.hidden = NO;
            self.rightTextField.placeholder = @"手动输入";
            self.rightTextField.text = productionPlanListFertlist.fertBrand;
            self.importanceLabel.hidden = YES;
            break;
        case 4:
            self.titleLabel.text = @"厂家";
            
            self.iconButton.hidden = YES;
            self.rightTextField.hidden = NO;
            self.rightTextField.placeholder = @"手动输入";
            self.rightTextField.text = productionPlanListFertlist.fertManu;
            self.importanceLabel.hidden = YES;
            break;
        case 5:
            self.titleLabel.text = @"价格";
            
            self.iconButton.hidden = YES;
            self.rightTextField.hidden = NO;
            self.rightTextField.placeholder = @"元/吨";
            self.rightTextField.text = productionPlanListFertlist.fertPrice;
            self.importanceLabel.hidden = NO;
            break;
        default:
            break;
    }
}

@end
